package ordersubmit

import (
	"mall/common/globalkey"
	"mall/common/resultx"
	// "github.com/zeromicro/go-zero/core/logx"
)

// ArgumentsHandler 校验参数的handler
type ArgumentsHandler struct {
	// 合成复用Next
	Next
}

// Do 校验参数的逻辑
func (h *ArgumentsHandler) Do(c *Context) (err error) {
	if len(c.Req.Skus) == 0 {
		return resultx.NewErrCode(resultx.PRODUCT_VARIATION_ID_REQUIRED)
	}

	var isVirtualOrder bool
	if c.Req.ShipmentId != nil {
		switch uint32(*c.Req.ShipmentId) {
		case globalkey.ShipmentTypeExpress:
			// todo
		case globalkey.ShipmentTypeVirtual:
			isVirtualOrder = true
		default:
			return resultx.NewErrCode(resultx.ORDER_SHIPMENT_ID_INVALID)
		}
	}

	if isVirtualOrder {
		c.OrderType = globalkey.OrderTypeVirtual
	} else {
		if c.Req.AddressId == nil || *c.Req.AddressId == 0 {
			return resultx.NewErrCode(resultx.USER_ADDRESS_ID_REQUIRED)
		}

		c.OrderType = globalkey.OrderTypeCommon
	}

	if c.Req.PaymentId != nil && *c.Req.PaymentId > 0 {
		switch uint32(*c.Req.PaymentId) {
		case globalkey.PaymentTypeWeChatPay:
			c.PaymentType = globalkey.PaymentTypeWeChatPay
		case globalkey.PaymentTypeGems:
			c.PaymentType = globalkey.PaymentTypeGems
		default:
			return resultx.NewErrCode(resultx.ORDER_PAYMENT_ID_INVALID)
		}
	}

	// logx.Infow("校验参数成功...")

	return
}
